<template>
  <div class="app-container supplier">
    <el-form :model="selectForm" ref="selectForm" :inline="true" label-width="120px">
      <el-form-item label="生产厂家名称：">
        <el-input size="small" v-model="selectForm.supplierName" placeholder="请输入生产厂家名称" clearable
                  @keyup.enter.native="getList"/>
      </el-form-item>
      <el-form-item label="生产厂家编号：">
        <el-input size="small" v-model="selectForm.supplierCode" placeholder="请输入生产厂家编号" clearable
                  @keyup.enter.native="getList"/>
      </el-form-item>
      <el-form-item>
        <el-button size="mini" type="primary" icon="el-icon-search" @click="getList"  v-hasPermi="['system:supplier:search']">搜索</el-button>
        <el-button size="mini" type="success" icon="el-icon-plus" @click="add"  v-hasPermi="['system:supplier:add']">新增</el-button>
        <el-button size="mini" type="warning" icon="el-icon-download" @click="exportList"  v-hasPermi="['system:supplier:export']">导出</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="list.rows" border v-loading="loading">
      <el-table-column align="center" type="index" label="序号" width="55"/>
      <el-table-column label="生产厂家名称" prop="supplierName" align="center"/>
      <el-table-column label="生产厂家编号" prop="supplierCode" align="center"/>
      <el-table-column label="手机号" prop="phone" align="center"/>
      <el-table-column label="联系人" prop="contacts" align="center"/>
      <el-table-column label="地址" prop="address" align="center" :show-overflow-tooltip="true"/>
      <el-table-column label="备注" prop="remark" align="center" :show-overflow-tooltip="true"/>
      <el-table-column label="操作" align="center" fixed="right" width="200px">
        <template slot-scope="scope">
          <el-button size="mini" type="success" icon="el-icon-edit" @click="edit(scope.row)" v-hasPermi="['system:supplier:edit']">修改</el-button>
          <el-button size="mini" type="danger" icon="el-icon-delete" @click="del(scope.row)" v-hasPermi="['system:supplier:del']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      :total="list.total?list.total:0"
      :page.sync="selectForm.pageNum"
      :limit.sync="selectForm.pageSize"
      @pagination="getList"
    />
    <el-dialog :close-on-click-modal="false" :title="addForm.id?'编辑生产厂家':'新增生产厂家'" :visible.sync="addVisible" width="500px">
      <el-form class="form" :model="addForm" ref="addForm" :inline="true" label-width="110px">
        <el-form-item label="生产厂家名称：" prop="supplierName"
                      :rules="[{required: true, message: '请输入生产厂家名称', trigger: 'blur'}]">
          <el-input size="small" v-model="addForm.supplierName" placeholder="请输入生产厂家名称" clearable/>
        </el-form-item>
        <el-form-item label="生产厂家编号：" prop="supplierCode">
          <el-input size="small" v-model="addForm.supplierCode" placeholder="请输入生产厂家编号" clearable/>
        </el-form-item>
        <el-form-item label="手机号：" prop="phone" >
<!--          :rules="[{required: true, message: '请输入手机号', trigger: 'blur'},{-->
<!--          pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,-->
<!--          message: '请输入正确的手机号码',-->
<!--          trigger: 'blur'}]"-->
          <el-input size="small" v-model="addForm.phone" placeholder="请输入手机号" clearable type="number"/>
        </el-form-item>
        <el-form-item label="联系人：" prop="contacts" >
          <el-input size="small" v-model="addForm.contacts" placeholder="请输入联系人" clearable/>
        </el-form-item>
        <el-form-item label="地址：" prop="address" >
          <el-input size="small" v-model="addForm.address" placeholder="请输入地址" type="textarea" :rows="3"/>
        </el-form-item>
        <el-form-item label="备注：" prop="remark" >
          <el-input size="small" v-model="addForm.remark" placeholder="请输入备注" type="textarea" :rows="3"/>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addVisible = false" size="mini">取消</el-button>
        <el-button type="primary" @click="onSumbit" size="mini">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import { supplier, supplierDel, supplierAdd, supplierEdit, supplierExport } from '@/api/basicInformation/index'

  export default {
    name: 'supplier.vue',
    data() {
      return {
        selectForm: {
          supplierName: '',
          supplierCode: '',
          isDelete: 1,
          pageNum: 1,
          pageSize: 10
        },
        addForm: {
          supplierName: '',
          supplierCode: '',
          phone: '',
          contacts: '',
          address: '',
          remark: ''
        },
        loading: false,
        list: '',
        addVisible: false
      }
    },
    created() {
      this.getList()
    },
    methods: {
      async getList() {
        this.loading = true
        let res = await supplier(this.selectForm)
        this.list = res
        this.loading = false
      },
      add() {
        delete this.addForm.id
        Object.assign(this.$data.addForm, this.$options.data().addForm)
        this.addVisible = true
      },
      edit(row) {
        this.addForm = {
          id: row.id,
          supplierName: row.supplierName,
          supplierCode: row.supplierCode,
          phone: row.phone,
          contacts: row.contacts,
          address: row.address,
          remark: row.remark
        }
        this.addVisible = true
      },
      onSumbit() {
        this.$refs['addForm'].validate(async(valid) => {
          if (valid) {
            this.addForm.id ? await supplierEdit(JSON.stringify(this.addForm)) : await supplierAdd(JSON.stringify(this.addForm))
            this.addForm.id ? this.$message.success('编辑成功') : this.$message.success('新增成功')
            this.addVisible = false
            this.getList()
          } else {
            // this.$message.info('数据不能为空')
            return false
          }
        })
      },
      del(row) {
        this.$confirm('确定删除吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async() => {
          await supplierDel(row.id)
          this.$message.success('删除成功')
          this.getList()
        }).catch(() => {
          this.$message.info('取消删除')
        })
      },

      /** 导出按钮操作 */
      exportList() {
        const queryParams = this.selectForm
        this.$confirm('是否确认导出所有用户数据项?', '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async() => {
          let res = await supplierExport(queryParams)
          return this.download(res.data)
        }).catch(function() {
        })
      }
    }
  }
</script>
